package com.fast.develop.api.rest;

import com.fast.develop.management.ManagementConfiguration;
import com.fast.develop.management.ResourceTreeManager;
import com.fast.develop.management.SchemaManager;
import com.fast.develop.management.resource.vo.TreeVo;
import com.fast.develop.meta.base.MetaConfiguration;
import com.fast.develop.meta.dm.MetaDomainConstant;
import com.fast.develop.meta.resource.MetaResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author 莫寅
 * @createTime 2022-06-11 03:26
 * @desc：
 */
@RequestMapping("schemas")
public class SchemaRest {


    private SchemaManager metaManager;
    private MetaConfiguration metaConfiguration;
    private ResourceTreeManager resourceTreeManager;

    public SchemaRest(ManagementConfiguration managementConfiguration) {
        this.metaConfiguration = managementConfiguration.getMetaConfiguration();
        this.metaManager = managementConfiguration.getResourceTreeManager().getSchemaManager();
        this.resourceTreeManager = managementConfiguration.getResourceTreeManager();
    }


    @GetMapping("{type}")
    public ResponseEntity<Map<String, MetaResource>> getSchemas(@PathVariable(name = "type") String type){
        Map<String, MetaResource> resources = metaConfiguration.getMetaModels(type);
        return new ResponseEntity(resources, HttpStatus.OK);
    }

    @GetMapping("meta/{type}")
    public ResponseEntity<Map> getMetaSchemas(@PathVariable(name = "type") String type, String path){
        Map resources = metaConfiguration.getMetaModels(type);
        return new ResponseEntity(resources, HttpStatus.OK);
    }

    @PostMapping("{meta}")
    public ResponseEntity<TreeVo> create(@PathVariable(name = "meta") String meta, String path){
        metaManager.create(meta, path);

        return new ResponseEntity(HttpStatus.OK);
    }


    @PostMapping("{meta}/fields")
    public ResponseEntity<TreeVo> createDoaminField(@PathVariable(name = "meta") String meta, String path){
        metaManager.create(MetaDomainConstant.Field_TYPE, path);

        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping("{meta}")
    public ResponseEntity<TreeVo> write(@PathVariable(name = "meta") String meta, String path, String value){
        metaManager.write(meta, path, value);

        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping("{meta}/enable")
    public ResponseEntity<TreeVo> enable(@PathVariable(name = "meta") String meta, String path){
        metaManager.enable(meta, path);

        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping("{meta}/disable")
    public ResponseEntity<TreeVo> disable(@PathVariable(name = "meta") String meta, String path){
        metaManager.disable(meta, path);

        return new ResponseEntity(HttpStatus.OK);
    }


}
